<?php
/**
 * class RegistryController
 *
 * Description for class RegistryController
 *
 * @author:
*/
class RegistryController extends Zend_Controller_Action {
	/**
	 * Initialize object
	 *
	 * @return void
	 */
	public function init() {
		/* Initialize action controller here */
	}
	
	/**
	 * indexAction
	 *
	 * @return void
	 */
	public function indexAction() {	
		$this->view->title = "System applications";
		$this->view->headTitle($this->view->title, 'API Server');
		
		try {
			$regs = new Model_DbTable_Registries();				
			$this->view->registries = $regs->fetchAll();
		} catch(Exception $e) {
			
		}
	}	
	
	/**
	 * This is method addAction
	 *
	 * @return mixed This is the return value description
	 *
	 */	
	public function addAction() {
		$this->view->title = "Add new application";
		$this->view->headTitle($this->view->title, 'API Server');
		
		$form = new Form_Registry();
		$form->submit->setLabel('Add');
		$this->view->form = $form;
		
		if ($this->getRequest()->isPost()) {			
			$userId = $form->getValue('user_id');
			
			$data = $this->_request->getPost();
			if ($form->isValid($data)) {				
				$callbackUri = $form->getValue('callback_uri');
				$requesterName = $form->getValue('requester_name');
				$requesterEmail = $form->getValue('requester_email');
				$appUri = $form->getValue('app_uri');
				$appTitle = $form->getValue('app_title');
				$appDesc = $form->getValue('app_desc');
				$appNotes = $form->getValue('app_notes');
				$appLogo = $form->getValue('app_logo');
				$appType = $form->getValue('app_type');
				$appCommercial = $form->getValue('app_commercial');
				
				$regs = new Model_DbTable_Registries();
				$regs->addRegistry($userId, 1, 'Y', $requesterName, 
					$requesterEmail, $callbackUri, $appUri, $appTitle, $appDesc, 
					$appNotes, $appLogo, $appType, $appCommercial);				
				
				$this->_redirect('/registry');
			} else {
				$form->populate($data);
			}
		}
	}	
	
	/**
	 * This is method editAction
	 *
	 * @return mixed This is the return value description
	 *
	 */	
	public function editAction() {
		$this->view->title = "Edit registry";
		$this->view->headTitle($this->view->title, 'API Server');
		
		$form = new Form_Registry();
		$form->submit->setLabel('Save');
		$this->view->form = $form;
		
		if ($this->getRequest()->isPost()) {
			$userId = $form->getValue('user_id');
			
			$data = $this->getRequest()->getPost();
			if ($form->isValid($data)) {
				$id = (int)$form->getValue('id');
				$enabled = $form->getValue('enabled');
				$status = $form->getValue('status');
				$requesterName = $form->getValue('requester_name');
				$requesterEmail = $form->getValue('requester_email');
				$callbackUri = $form->getValue('callback_uri');
				$appUri = $form->getValue('app_uri');
				$appTitle = $form->getValue('app_title');
				$appDesc = $form->getValue('app_desc');
				$appNotes = $form->getValue('app_notes');
				$appLogo = $form->getValue('app_logo');
				$appType = $form->getValue('app_type');
				$appCommercial = $form->getValue('app_commercial');
				
				$regs = new Model_DbTable_Registries();
				
				$regs->updateRegistry($id, $userId, $enabled, $status, 
					$requesterName, $requesterEmail, $callbackUri, $appUri, 
					$appTitle, $appDesc, $appNotes, $appLogo, $appType, 
					$appCommercial, date('Y-m-d H:i:s'));
				
				$this->_redirect('/registry');
			} else {
				$form->populate($data);
			}
		} else {
			$id = $this->_request->getParam('id', 0);
			if ($id > 0) {
				$regs = new Model_DbTable_Registries();
				$form->populate($regs->getRegistry($id));
			}
		}   
	}	
	
	/**
	 * This is method deleteAction
	 *
	 * @return mixed This is the return value description
	 *
	 */	
	public function deleteAction() {
		$this->view->title = "Delete application";
		$this->view->headTitle($this->view->title, 'API Server');
		
		if ($this->getRequest()->isPost()) {
			$del = $this->getRequest()->getPost('del');
			if ($del == 'Yes') { 
				$id = $this->getRequest()->getPost('id');
				$regs = new Model_DbTable_Registries();
				$regs->deleteRegistry($id);
			}
			$this->_redirect('/registry');
		} else {
			$id = $this->_getParam('id', 0);
			$regs = new Model_DbTable_Registries();
			$this->view->registry = $regs->getRegistry($id);
		}
	}
}

?>